﻿//TODO: Contour: Implement

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Contour - this will show a good result when the input is a gray or black&white
    /// Otherwise you might see no result or only in party that are gray or black&white
    /// </summary>
    public class Contour : BaseImageFilter
    {
        protected Smooth m_Smooth = new Smooth();
        protected Dilate m_Dilate = new Dilate();
        protected ITwoSourceFilter m_Sub = new Sub();

        public Contour()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Contour"/> class.
        /// </summary>
        /// <param name="_contour">The _contour.</param>
        internal Contour(Contour _contour)
        {
        }

        /// <summary>
        /// Smooth
        /// </summary>
        public Smooth Smooth
        {
            get { return m_Smooth; }
            set { m_Smooth = value; }
        }

        /// <summary>
        /// Dilate
        /// </summary>
        public Dilate Dilate
        {
            get { return m_Dilate; }
            set { m_Dilate = value; }
        }
        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = (UnsafeBitmap)_bitmap.Clone();
            m_Smooth.Execute(bitmap);
            m_Dilate.Execute(bitmap);

            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    Color color1 = _bitmap.GetPixel(x, y);
                    Color color2 = bitmap.GetPixel(x, y);
                    _bitmap.SetPixelDirect(x, y, (byte)(ImageMath.Clamp(color1.R - color2.R, 0, 255)), (byte)(ImageMath.Clamp(color1.G - color2.G, 0, 255)), (byte)(ImageMath.Clamp(color1.B - color2.B, 0, 255)));
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Contour(this);
        }
    }
}
